<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class PostTransfer extends Command
{
    protected $link = 'https://api.telegram.org/bot';
    protected $apikey = '6184819631:AAGIR2rruihpMesbjZY6f4fJOjT4o5X_62k';
    protected $chat_id = '-918793265';
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'PostTransfer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //需要发送通知的信息
        $text = '';
        $info = DB::table('meta_switch')->where('notice_switch','=',1)->get();
        if (!$info){
            return;
        }
        foreach ($info as $key=>$value){
            $value->number = DB::table('meta_accounts')->where('appId','=',$value->appid)->count();
            $notice = $value->notice?"({$value->notice})":'';
            if ($key==0){
                $text .= urlencode(date('Y-m-d H:i:s',time()))."%0A%0A{$value->appid}{$notice}%0A地区:{$value->allow_location}%0A当前一共的转换：{$value->number}%0A";
            }else{
                $text .= "%0A{$value->appid}{$notice}%0A地区:{$value->allow_location}%0A当前一共的转换：{$value->number}%0A";
            }
        }
        $url = "{$this->link}{$this->apikey}/sendMessage?chat_id={$this->chat_id}&text={$text}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36');
        $res = curl_exec($ch);
        curl_close($ch);
    }
}
